package com.uniplaza.app.base;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.View;

import com.android.volley.RequestQueue;
import com.android.volley.components.volley.EventCallback;
import com.umeng.analytics.MobclickAgent;
import com.uniplaza.app.utils.Utils;
import com.uniplaza.app.volley.ErrorToolKit;


/**
 * 所有 Fragment 的父类  进行必要的验证
 * Created by Kim on 16/6/6.
 */
public abstract class BaseFragment extends Fragment implements EventCallback, View.OnClickListener {
    private static final String TAG = "BaseFragment";
    protected static RequestQueue mQueue;
    protected Context mContext;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = context;
        mQueue = Application.getInstance().getmQueue();
        Utils.logError(TAG, " onAttach --> " + getClass().getSimpleName());
    }


    @Override
    public void onResume() {
//        Utils.logError(TAG, " onResume --> " + getClass().getSimpleName());
        super.onResume();
        MobclickAgent.onPageStart(getClass().getName());
    }


    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getName());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mQueue.cancelAll(this);
    }


    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
//        activity.overridePendingTransition(R.anim.popup_right_in, R.anim.jump_static_out);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
//        activity.overridePendingTransition(R.anim.popup_right_in, R.anim.jump_static_out);
    }

    /**
     * 获取权限  但是必须重写permissionResutl 方法获取返回值
     */
    public boolean getPermission(String[] permission, int CallBackNum) {
        int p = 0;
        boolean istrue;
        istrue = true;

        for (int i = 0; i < permission.length; i++) {
            p = ContextCompat.checkSelfPermission(getActivity(), permission[i]);
            Utils.log(TAG, "权限" + i + "----" + p);
            if (p != PackageManager.PERMISSION_GRANTED) {//判断是没有此权限
                Utils.log(TAG, "是这个没有权限");
                istrue = false;
            }
        }

        Utils.log(TAG, "权限----" + "----" + istrue);

        if (!istrue) {
            ActivityCompat.requestPermissions(getActivity(), permission, CallBackNum);
            return false;
        } else {
            permissionResutl(true, CallBackNum);
            return true;
        }

    }


    @Override
    public void errorNetWork(String nRequestType, int error, String message) {
        ErrorToolKit.tostError(getActivity(), error, message);
    }

    /**
     * 获取权限 请重写permissionResutl 方法
     */
    protected void permissionResutl(boolean result, int CallBackNum) {
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            this.permissionResutl(true, requestCode);
        } else {
            this.permissionResutl(false, requestCode);
        }
    }


}
